package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"
)

var client = http.Client{
	Timeout: 10 * time.Second,
}

func HttpPostJson(url string, data interface{}, result interface{}, header map[string]string) error {
	buf := bytes.NewBuffer(nil)
	encoder := json.NewEncoder(buf)
	if err := encoder.Encode(data); err != nil {
		return err
	}

	request, err := http.NewRequest(http.MethodPost, url, buf)
	if err != nil {
		return err
	}

	request.Header.Add("Content-Type", "application/json")
	if header != nil {
		for k, v := range header {
			request.Header.Add(k, v)
		}
	}

	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	respBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return err
	}
	fmt.Println(respBody)
	fmt.Println(string(respBody))
	//decoder := json.NewDecoder(response.Body)
	//if err = decoder.Decode(&result); err != nil {
	//	return err
	//}

	return nil
}

func HttpPostForm(posturl string, data url.Values, result interface{}, host string) (error,[]byte) {
	request, err := http.NewRequest(http.MethodPost, posturl, strings.NewReader(data.Encode()))
	if err != nil {
		return err,nil
	}

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//request.Host = host

	resp, err := client.Do(request)
	if err != nil {
		return err,nil
	}
	defer resp.Body.Close()

	//decoder := json.NewDecoder(resp.Body)
	//if err = decoder.Decode(&result); err != nil {
	//	return err,nil
	//}
	respBody, err := ioutil.ReadAll(resp.Body)

	return nil,respBody
}

func main() {
	sl1 := make([]int, 3)
	fmt.Println(sl1)
	sl2 := []int{1,2,3,4,5}
	copy(sl1, sl2)
	fmt.Println(sl1)
	fmt.Println(sl2)
}